package com.qzsoft.zhjd.server.modules.ludong.exp.controller;


import com.qzsoft.zhjd.server.common.utils.MapToBeanUtils;
import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectLaboratoryDeviceEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.service.CollectExpFurnaceService;
import com.qzsoft.zhjd.server.modules.ludong.exp.entity.CollectExpFurnaceEntity;
import com.qzsoft.zhjd.server.modules.ludong.exp.service.CollectLaboratoryDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 *
 *功能描述：燃烧炉
 * @author jiangyafei
 * @email jiangyf_sd@163.com
 * @date 2020-06-09 11:17:30
 */
@Slf4j
@RestController
@RequestMapping("datacollect/exp/expfurnace")
public class CollectExpFurnaceController {
    @Autowired
    private CollectExpFurnaceService collectExpFurnaceService;
    @Autowired
    private CollectLaboratoryDeviceService deviceService;


    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestParam Map<String,Object> params){
        log.info("燃烧炉==="+params.toString());
        try{
            Map<String,Object> map = MapToBeanUtils.toReplaceKeyLow(params);
            CollectExpFurnaceEntity collectExpFurnaceEntity = MapToBeanUtils.mapToObject(map, CollectExpFurnaceEntity.class);
            if (StringUtils.isNotBlank(collectExpFurnaceEntity.getId())){
                collectExpFurnaceService.insertAndRemoveData(collectExpFurnaceEntity);
                return R.ok();
            }
            return R.error(400,"数据为空或id为空！");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return R.error(400,e.getMessage());
        }

    }

    @PostMapping("/furnaceInsert")
    public R furnaceInsert(@RequestBody Map<String,Object> params){
        try{
            CollectExpFurnaceEntity collectExpFurnaceEntity = MapToBeanUtils.mapToCapitalObject(params, CollectExpFurnaceEntity.class);
            collectExpFurnaceEntity.setEquipmentNumber(params.get("DEVICENO").toString());
            //查询设备编号
            QueryWrapper<CollectLaboratoryDeviceEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("unique_code",params.get("DEVICENO").toString());
            List<CollectLaboratoryDeviceEntity> list = deviceService.list(queryWrapper);
            if (list.size()>0) {
                collectExpFurnaceEntity.setEquipmentName(list.get(0).getName());
            }
            collectExpFurnaceEntity.setUploadTime(new Date());
            collectExpFurnaceService.insertAndRemoveData(collectExpFurnaceEntity);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return R.error(400,e.getMessage());
        }

    }


}
